//
//  HWMyYouXiViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/10/23.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWMyYouXiViewController.h"
#import "HWGameCollectYXCell.h" // 我的游戏
#import "HWGameCollectModel.h" // 游戏模型
 


@interface HWMyYouXiViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(strong,nonatomic)UITableView *mainaTableVie;
@property(strong,nonatomic) NSMutableArray *dataMuArr;
@property(strong,nonatomic) NSString *p;
@property(strong,nonatomic) NSString *max_p;

@property(strong,nonatomic) UIImageView *defaultIamegView;
@property(strong,nonatomic) UILabel *wuLabel;

@end

@implementation HWMyYouXiViewController

-(UITableView *)mainaTableVie {

    if  (_mainaTableVie == nil) {

        _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH-NavBarHeight - 127) style:UITableViewStylePlain];
        _mainaTableVie.delegate = self;
        _mainaTableVie.dataSource = self;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _mainaTableVie.backgroundColor = [UIColor whiteColor];
       // _mainaTableVie.backgroundColor = RGB(241, 242, 244);
        
    }
    return   _mainaTableVie ;

}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addControlsView];
    
    //发送网络请求
    [self addrefreshControl];
    
    [self zanWuNoDdataIcoImageView];
    
    
}



- (void)zanWuNoDdataIcoImageView {
    
    self.defaultIamegView = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenW-189)/2,(kScreenH-NavBarHeight - 127)/2-15-80, 189, 124)];
    self.defaultIamegView.image = [UIImage imageNamed:@"details_dealbg_NOdata"];
    [self.mainaTableVie addSubview: self.defaultIamegView];
    
    
    self.wuLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, self.defaultIamegView.bottom, kScreenW, 15)];
    self.wuLabel.text = @"暂无数据";
    self.wuLabel.font = [UIFont systemFontOfSize:13];
    self.wuLabel.textColor = RGB(102, 102, 102);
    self.wuLabel.textAlignment  = NSTextAlignmentCenter;
    [self.mainaTableVie addSubview: self.wuLabel];
    
    
    
    
}






#pragma  mark ---- 添加刷新

- (void)addrefreshControl {
   
    self.mainaTableVie.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
    
   [self.mainaTableVie.mj_header beginRefreshing];
    
    self.mainaTableVie.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
}

// 下拉刷新
- (void)underRefresh {
    
   
    
    self.p = @"1";
     
    NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
    NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
    
    NSString *typeStr =  [NSString stringWithFormat:@"%@",@"0"];
    
    NSString *singStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];
    
    NSString *singMD5 = [WMUtil md5HexDigest:singStr];

    self.dataMuArr = [[NSMutableArray alloc]init];
    NSDictionary *parametes = @{
                                       @"p" :self.p,
                                       @"ios" : @"1",
                                       @"ct" :  @"app",
                                       @"ac" : @"mycollect",
                                       @"uid" :uidStr,
                                       @"t" : tStr,
                                       @"type" :typeStr,
                                       @"sign" : singMD5
                                       };
       
       [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
           
           // [SVProgressHUD dismiss];
           [self.mainaTableVie.mj_header endRefreshing];
           [self.mainaTableVie.mj_footer endRefreshing];
           
           
           NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
           
           if ([codeStr intValue] == 1) {
               
              self.dataMuArr = [HWGameCollectModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];
               self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
               self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
               
               self.defaultIamegView.hidden = YES;
               self.wuLabel.hidden = YES;
               
               
            
               
    
           }  else {
               
               
               
               
           }
           
           
        
           [self.mainaTableVie reloadData];
           
           
       } failure:^(NSString *error) {
           [self.mainaTableVie.mj_header endRefreshing];
           [self.mainaTableVie.mj_footer endRefreshing];
           
           
       }];
    
    
}

// 上拉加载

- (void)onRefresh {
    
    
   
    int p = [self.p intValue];
                   
    int max_p = [self.max_p intValue];
                   
     if (p > max_p) {
        [MBProgressHUD showError:@"没有更多数据，请待会在上拉加载"];
                       
        return ;
                       
      }
                   
    
    
    NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
     NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
     
     NSString *typeStr =  [NSString stringWithFormat:@"%@",@"0"];
     
     NSString *singStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];
     
     NSString *singMD5 = [WMUtil md5HexDigest:singStr];

  
     NSDictionary *parametes = @{
                                         @"p" :self.p,
                                         @"ios" : @"1",
                                         @"ct" :  @"app",
                                         @"ac" : @"mycollect",
                                         @"uid" :uidStr,
                                         @"t" : tStr,
                                         @"type" :typeStr,
                                         @"sign" : singMD5
                                         };
         
        [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
             
             // [SVProgressHUD dismiss];
             [self.mainaTableVie.mj_header endRefreshing];
             [self.mainaTableVie.mj_footer endRefreshing];
             
             
             NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
             
             if ([codeStr intValue] == 1) {
                 
             
               
                
                 NSMutableArray *mutableArr = [HWGameCollectModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];


                 for (int i = 0; i < mutableArr.count; i++) {
                     [self.dataMuArr addObject:mutableArr[i]];

                 }
                 
                 self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                 self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
                 [self.mainaTableVie reloadData];
                 
      
             }
          
             
             
         } failure:^(NSString *error) {
             [self.mainaTableVie.mj_header endRefreshing];
             [self.mainaTableVie.mj_footer endRefreshing];
             
             
         }];
    
    
    
    
}







// 添加控件
-(void)addControlsView {
    
    
    self.view.backgroundColor = [UIColor orangeColor];
    self.navigationItem.title = @"我的收藏";
    
    [self.view addSubview:self.mainaTableVie];
    
    
}






#pragma mark ------------  UITableViewDataSource


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    
    return self.dataMuArr.count;
    
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"cellID";
    HWGameCollectYXCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"HWGameCollectYXCell" owner:nil options:nil].firstObject;
        
    }
   // cell.textLabel.text = @"icdihohio'o";
    
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    cell.model = self.dataMuArr[indexPath.row];
//    cell.shoucangBut.tag = indexPath.row;
//    cell.shoucangBut.userInteractionEnabled = YES;
//    cell.shoucangBut.enabled = YES;
    cell.shanchuBtn.tag = indexPath.row;
    [cell.shanchuBtn addTarget:self action:@selector(shanchuBtnTap:) forControlEvents:UIControlEventTouchUpInside];
    
    return  cell;
    
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
   
    
    return  71;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HWGameDetailController *GameDetails = [[HWGameDetailController alloc]init];
    
    HWGameCollectModel *model = self.dataMuArr[indexPath.row];
    GameDetails.gameId = model.gid;
    [self.navigationController pushViewController:GameDetails animated:YES];
    
    
    
}




- (void)shanchuBtnTap:(UIButton *)sender {
    
    NSLog(@"nishi  %d",sender.tag);
    
      HWGameCollectModel *model = self.dataMuArr[sender.tag];
    
    
          NSString *uid = [HWLocallyStoredData getLoginUserID];
          NSString *tString = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
          NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tString,@"fsd213ewdsadqwe2121213edsad"];
          NSString *signMD5 = [WMUtil md5HexDigest:signStr];
          NSString *idStr = model.id;
    
    
    
    
    
          NSDictionary *parametes = @{
                            @"uid":uid,
                            @"t":tString,
                            @"sign":signMD5,
                            @"id": idStr,
                            @"ios" : @"1",
                            @"ct":@"app",
                            @"type":@"0",
                            @"ac":@"shoucang_del"};
    
    
    
    
    
           [MBProgressHUD show:@"取消收藏"];
    
           [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
    
    
    
               NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
    
               if ([codeStr intValue] == 1) {
                  // [MBProgressHUD showMessage:@"收藏成功" toView:self.view];
    
                //   [MBProgressHUD showSuccess:@"取消收藏成功"];
                    [self.mainaTableVie.mj_header beginRefreshing];
    
    
               } else {
    
    
                  // [MBProgressHUD showError:@"收藏失败"];
                  [MBProgressHUD showError:response[@"message"]];
    
               }
    
    
           } failure:^(NSString *error) {
    
    
               [MBProgressHUD showSuccess:@"取消收藏失败"];
    
           }];
    
    
        
    
    
    
    
}

- (void)quexiaoCollection {
    
    
    
    
//      NSString *uid = [HWLocallyStoredData getLoginUserID];
//      NSString *tString = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
//      NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tString,@"fsd213ewdsadqwe2121213edsad"];
//      NSString *signMD5 = [WMUtil md5HexDigest:signStr];
//      NSString *idStr = self.topModel.shoucang_id;
//
//
//
//
//
//      NSDictionary *parametes = @{
//                        @"uid":uid,
//                        @"t":tString,
//                        @"sign":signMD5,
//                        @"id": idStr,
//                        @"ios" : @"1",
//                        @"ct":@"app",
//                        @"type":@"0",
//                        @"ac":@"shoucang_del"};
//
//
//
//
//
//       [MBProgressHUD show:@"取消收藏"];
//
//       [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
//
//
//
//           NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
//
//           if ([codeStr intValue] == 1) {
//              // [MBProgressHUD showMessage:@"收藏成功" toView:self.view];
//
//               [MBProgressHUD showSuccess:@"取消收藏成功"];
//
//
//
//           } else {
//
//
//              // [MBProgressHUD showError:@"收藏失败"];
//              [MBProgressHUD showError:response[@"message"]];
//
//           }
//
//
//       } failure:^(NSString *error) {
//
//
//           [MBProgressHUD showSuccess:@"取消收藏失败"];
//
//       }];
//
//
    
    
    
    
    
    
    
}


@end
